package cn.mafangui.hotel.service.impl;

import cn.mafangui.hotel.entity.GarageAccessRecord;
import cn.mafangui.hotel.entity.GarageStall;
import cn.mafangui.hotel.entity.User;
import cn.mafangui.hotel.enums.ExceptionEnum;
import cn.mafangui.hotel.enums.GarageAccessStatusEnum;
import cn.mafangui.hotel.enums.StallStausEnum;
import cn.mafangui.hotel.exception.HotelException;
import cn.mafangui.hotel.mapper.GarageAccessRecordMapper;
import cn.mafangui.hotel.mapper.GarageStallMapper;
import cn.mafangui.hotel.req.PageQueryReq;
import cn.mafangui.hotel.req.ParkingInReq;
import cn.mafangui.hotel.service.GarageAccessRecordService;
import cn.mafangui.hotel.service.GarageStallService;
import cn.mafangui.hotel.service.UserService;
import cn.mafangui.hotel.utils.AssertUtils;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * @author: kalpa
 * @date: 2022/3/19
 * @version: 1.0
 * Description: No Description
 */
@Service
public class GarageAccessRecordServiceImpl implements GarageAccessRecordService {
    @Resource
    GarageAccessRecordMapper garageAccessRecordMapper;

    @Resource
    GarageStallMapper garageStallMapper;


    @Autowired
    UserService userService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean in(ParkingInReq req) throws HotelException {
        // 1. 查询到用户的信息
        User user = userService.selectById(req.getUserId());
        AssertUtils.assertTrue(!ObjectUtils.isEmpty(user), ExceptionEnum.USER_NOT_FOUND);

        // 2. 查询车位信息
        GarageStall garageStall = garageStallMapper.selectById(req.getStallId());
        AssertUtils.assertTrue(!ObjectUtils.isEmpty(garageStall), ExceptionEnum.USER_NOT_FOUND);

        // 2. 停车入库
        GarageAccessRecord garageAccessRecord = new GarageAccessRecord();
        BeanUtils.copyProperties(req, garageAccessRecord);
        garageAccessRecord.setName(user.getName());
        garageAccessRecord.setStallLocation(garageStall.getLocation());
        garageAccessRecord.setInDate(new Date());
        garageAccessRecord.setStatus(GarageAccessStatusEnum.ONGOING.getCode());
        int add = garageAccessRecordMapper.add(garageAccessRecord);
        AssertUtils.assertTrue(add != 1, ExceptionEnum.GARAGE_RECORD_ADD_FAIED);

        // 3. 更新车位的状态
        int i = garageStallMapper.updateStatus(req.getStallId(), StallStausEnum.STOPED.getCode());
        AssertUtils.assertTrue(i != 1, ExceptionEnum.STALL_UPDATE_FAIED);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean out(Integer id) throws HotelException {
        // 1. 找到车位
        GarageStall garageStall = garageStallMapper.selectById(id);
        AssertUtils.assertTrue(!ObjectUtils.isEmpty(garageStall), ExceptionEnum.USER_NOT_FOUND);

        // 2. 找到订单
        GarageAccessRecord garageAccessRecordQuery = new GarageAccessRecord();
        garageAccessRecordQuery.setStallId(id);
        List<GarageAccessRecord> garageAccessRecordList = garageAccessRecordMapper.query(garageAccessRecordQuery);
        AssertUtils.assertTrue(!CollectionUtils.isEmpty(garageAccessRecordList), ExceptionEnum.GARAGE_RECORD_NOT_FOUND);
        GarageAccessRecord garageAccessRecord = garageAccessRecordList.get(0);

        // 3. 更新车库订单
        garageAccessRecord.setStatus(GarageAccessStatusEnum.FINISH.getCode());
        garageAccessRecord.setOutDate(new Date());
        int update = garageAccessRecordMapper.update(garageAccessRecord);
        AssertUtils.assertTrue(update != 1, ExceptionEnum.GARAGE_RECORD_UPDATE_FAIED);

        // 4. 更新车位信息
        int i = garageStallMapper.updateStatus(garageStall.getId(), StallStausEnum.FREE.getCode());
        AssertUtils.assertTrue(i != 1, ExceptionEnum.STALL_UPDATE_FAIED);

        return true;
    }

    @Override
    public List<GarageAccessRecord> getAllByPage(PageQueryReq pageQueryReq) {
        PageHelper.startPage(pageQueryReq.getPage(), pageQueryReq.getSize());
        return  garageAccessRecordMapper.getAll();
    }

}
